package com.mapper;

import com.bean.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/* 与user表 相关的增删改查 */
public interface UserMapper {
    /* 查询用户
    *   注解上添加的就是要执行的sql语句
    *   如果只有一个参数,则value可以直接省略不写
    *  */
    //@Select(value = "select * from user")
    @Select("select * from user")
    public List<User> findAll();

    /*添加用户*/
    @Insert("insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})")
    public void saveUser(User user);

    /*更新用户*/
    @Update("update user set username = #{username},birthday = #{birthday} where id = #{id}")
    public void update(User user);

    /*删除用户 - 根据id删除*/
    @Delete("delete from user where id = #{id}")
    public void delete(Integer id);


    /*一对一查询: 根据id查询用户 */
    @Select("select * from user where id = #{uid}")
    public User findById(Integer uid);

    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "username",column = "username"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "address",column = "address"),
            @Result(
                    property = "ordersList",
                    javaType = List.class,
                    column = "id",
                    many = @Many(
                            select = "com.mapper.OrderMapper.find",
                            fetchType = FetchType.LAZY
                    )
            )
    })
    public List<User> findBy();

    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "username",column = "username"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "address",column = "address"),
            @Result(
                    property = "roleList",
                    javaType = List.class,
                    column = "id",
                    many = @Many(
                            select = "com.mapper.RoleMapper.findByUid",
                            fetchType = FetchType.EAGER
                    )
            )
    })
    public List<User> findAllRole();
}
